#' Estimation for 2 regime s
#'
#' @param vardata a data frame for a single country
#' @param OrderThr a ordered threshold variable
#' @param endovar_temp endonegous variables
#' @param thrsholdvar
#' @param p lag order of VAR
#' @param trim ratio for truncation in TGVAR
updwEst <- function(vardata, OrderThr, endovar_tmp, thrsholdvar, exogen, p =2, trim = 0.3){
  tvarrlt <- list()
  tmpaic <- NULL
  i <- 1
  for (thr in OrderThr[round(length(OrderThr) * trim):round(length(OrderThr) * (1 - trim))]) {
    dn <- VARDN(vardata[,endovar_tmp], p = p,type = 'const', thV = dplyr::lag(vardata[,thrsholdvar]),
                thVValue = thr,exogen = exogen)
    up <- VARUP(vardata[,endovar_tmp], p = p,type = 'const', thV = dplyr::lag(vardata[,thrsholdvar]),
                thVValue = thr,exogen = exogen)
    tmpaic <- det(cov(rbind(residuals(dn),residuals(up)))) %>% log() %>% c(tmpaic,.)
    tvarrlt[[i]] <- list(dn = dn, up = up, aic = tmpaic[length(tmpaic)], thr = thr)
    i <-  i + 1
  }
  return(list(tvarrlt = tvarrlt, tmpaic = tmpaic))
}
